package com.jiuzhou.guanwang.jzcp.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jiuzhou.guanwang.jzcp.R;
import com.jiuzhou.guanwang.jzcp.bean.MatchInfo;

import java.util.List;

public class JczqAdapter extends BaseAdapter {
    private final Context mContext;
    private List<MatchInfo> mData;

    class ViewHolder {
        TextView mMatchTv;
        TextView mScoreA;
        TextView mScoreB;
        TextView mStatus;
        TextView mTeamNameA;
        TextView mTeamNameB;
        TextView mTime;
        ImageView iv_host;
        ImageView iv_visitor;

        ViewHolder() {
        }
    }

    public JczqAdapter(Context context) {
        this.mContext = context;
    }

    public int getCount() {
        return this.mData == null ? 0 : this.mData.size();
    }

    public Object getItem(int position) {
        if (this.mData != null && position < this.mData.size()) {
            return this.mData.get(position);
        }
        return null;
    }

    public long getItemId(int position) {
        return (long) position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(this.mContext).inflate(R.layout.item_lq, parent, false);
            viewHolder.mMatchTv = (TextView) convertView.findViewById(R.id.tv_match);
            viewHolder.mStatus = (TextView) convertView.findViewById(R.id.tv_status);
            viewHolder.mTeamNameA = (TextView) convertView.findViewById(R.id.tv_name_a);
            viewHolder.mScoreA = (TextView) convertView.findViewById(R.id.tv_score_a);
            viewHolder.mTeamNameB = (TextView) convertView.findViewById(R.id.tv_name_b);
            viewHolder.mScoreB = (TextView) convertView.findViewById(R.id.tv_score_b);
            viewHolder.mTime = (TextView) convertView.findViewById(R.id.tv_time);
            viewHolder.iv_host = (ImageView) convertView.findViewById(R.id.iv_host);
            viewHolder.iv_visitor = (ImageView) convertView.findViewById(R.id.iv_visitor);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        MatchInfo matchInfo = (MatchInfo) this.mData.get(position);
        if (!TextUtils.isEmpty(matchInfo.statusDesc)) {
            viewHolder.mStatus.setText(matchInfo.statusDesc);
            if ("直播中".equals(matchInfo.statusDesc)) {
                viewHolder.mStatus.setTextColor(this.mContext.getResources().getColor(R.color.green));
            } else if ("已结束".equals(matchInfo.statusDesc)) {
                viewHolder.mStatus.setTextColor(this.mContext.getResources().getColor(R.color.orange));
            }
        }
        if (!TextUtils.isEmpty(matchInfo.leagueName)) {
            viewHolder.mMatchTv.setText(matchInfo.leagueName);
        }
        if (!TextUtils.isEmpty(matchInfo.teamA)) {
            viewHolder.mTeamNameA.setText(matchInfo.teamA);
        }
        if (!TextUtils.isEmpty(matchInfo.teamAGoal)) {
            viewHolder.mScoreA.setText(matchInfo.teamAGoal);
        }
        if (!TextUtils.isEmpty(matchInfo.teamBGoal)) {
            viewHolder.mScoreB.setText(matchInfo.teamBGoal);
        }
        if (!TextUtils.isEmpty(matchInfo.teamB)) {
            viewHolder.mTeamNameB.setText(matchInfo.teamB);
        }
        if (!TextUtils.isEmpty(matchInfo.matchDay)) {
            viewHolder.mTime.setText(matchInfo.matchTime);
        }
        if (!TextUtils.isEmpty(matchInfo.teamALogo)) {
            Glide.with(this.mContext).load(matchInfo.teamALogo).into(viewHolder.iv_host);
        }
        if (!TextUtils.isEmpty(matchInfo.teamALogo)) {
            Glide.with(this.mContext).load(matchInfo.teamBLogo).into(viewHolder.iv_visitor);
        }
        return convertView;
    }

    public void setData(List<MatchInfo> matchInfo) {
        this.mData = matchInfo;
        notifyDataSetChanged();
    }
}